package me.keiwu.user.controller;

import com.alibaba.fastjson.JSONObject;
import me.keiwu.pub.data.ResponseV2;
import me.keiwu.pub.data.wx.WXGroupData;
import me.keiwu.pub.data.wx.WXSessionData;
import me.keiwu.pub.service.WXService;
import me.keiwu.pub.util.CacheUtil;
import me.keiwu.pub.util.ContextUtil;
import me.keiwu.user.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by kei on 08/02/2018.
 */
@RestController
@RequestMapping("/rank")
public class RankController {

    @Autowired
    private UserService userService;
    @Autowired
    private WXService wxService;
    @Autowired
    private CacheUtil cacheUtil;

    @RequestMapping("get")
    public ResponseV2 getRank(Long start, Long end) {
        return userService.getRank(start, end);
    }

    @RequestMapping("myRank")
    public ResponseV2 myRank(HttpServletRequest request) {
        Long userId = ContextUtil.getLoginUserId(request);
        return userService.getUserRank(userId);
    }


    @RequestMapping("multi-get")
    public ResponseV2 rank(String idList) {
        List<JSONObject> list = new ArrayList<>();
        for (String s : StringUtils.split(idList, ",")) {
            s = s.trim();
            if (StringUtils.isBlank(s)) continue;
            Long id = Long.valueOf(s);
            JSONObject json = new JSONObject();
            json.put("userInfo", userService.getUserInfo(id));
            json.put("rank", cacheUtil.getUserRank(id));
            json.put("score", cacheUtil.getRankScore(id));
            list.add(json);
        }
        return ResponseV2.ok(list);
    }


    @RequestMapping("joinGroupRank")
    public ResponseV2 joinGroupRank(HttpServletRequest request, String encryptedData, String iv) {
        Long userId = ContextUtil.getLoginUserId(request);
        WXSessionData session = cacheUtil.getWXSession(userId);
        if (session == null) return ResponseV2.fail("not login");

        WXGroupData data = wxService.decryptData(encryptedData, iv, session.getSessionKey(), WXGroupData.class);
        String openGId = data.getOpenGId();
        // 前端解析，所以没有
        String groupName = "";
        return userService.joinGroupRank(openGId, userId, groupName);
    }

    @RequestMapping("myGroupList")
    public ResponseV2 myGroupList(HttpServletRequest request,
                                  @RequestParam(defaultValue = "0") Integer page,
                                  @RequestParam(defaultValue = "10") Integer size) {
        Long userId = ContextUtil.getLoginUserId(request);

        return userService.getUserGroupList(userId, page, size);
    }


    @RequestMapping("groupRank")
    public ResponseV2 groupRank(String openGId, Long start, Long end) {
        return userService.getGroupRank(openGId, start, end);
    }

    @RequestMapping("myGroupRank")
    public ResponseV2 myGroupRank(HttpServletRequest request, String openGId) {
        Long userId = ContextUtil.getLoginUserId(request);
        return userService.getUserGroupRank(openGId, userId);
    }

}
